package registration;


import java.util.Random;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import org.testng.*;
import org.testng.annotations.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Script extends ru.st.selenium.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();
 
    @Test
  public void testUntitled5() throws Exception {
    	Random rand = new Random();

        String AlphabetString = "ABCDEFGHIKLMNOPQRSTVXYZabcdefghiklmnopqrstvxyz";

        char [] alphabet = AlphabetString.toCharArray();
        
        StringBuilder generator = new StringBuilder(); 

        for(int i = 0; i < 7; i++){
        	generator.append(alphabet[rand.nextInt(alphabet.length)]);
           
        }
        String email = generator.toString();
    	
    driver.get(baseUrl + "/");
    driver.findElement(By.linkText("Log In")).click();
    driver.findElement(By.cssSelector("div.buttons-set > button.button")).click();
    driver.findElement(By.id("firstname")).clear();
    driver.findElement(By.id("firstname")).sendKeys("<script>alert(\"hello2\");</script>");
    driver.findElement(By.id("lastname")).clear();
    driver.findElement(By.id("lastname")).sendKeys("<script>alert(\"hello2\");</script>");
    driver.findElement(By.id("email_address")).clear();
    driver.findElement(By.id("email_address")).sendKeys(email+"@"+email+".com");
    driver.findElement(By.id("password")).clear();
    driver.findElement(By.id("password")).sendKeys("<script>alert(\"hello2\");</script>");
    driver.findElement(By.id("confirmation")).clear();
    driver.findElement(By.id("confirmation")).sendKeys("<script>alert(\"hello2\");</script>");
    driver.findElement(By.cssSelector("div.buttons-set > button.button")).click();
    try {
      assertEquals("Thank you for registering with Main Store.", driver.findElement(By.cssSelector("li > span")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    driver.findElement(By.linkText("Log Out")).click();
  }

   private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  
  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}
